﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;

	public class ResourceService : IResourceService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceService"/> class.
		/// </summary>
		/// <param name="resourceRepository">The resource repository.</param>
		public ResourceService(IResourceRepository resourceRepository)
		{
			this.ResourceRepository = resourceRepository;
		}

		/// <summary>
		/// Gets or sets the resource repository.
		/// </summary>
		/// <value>The resource repository.</value>
		public IResourceRepository ResourceRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the resource by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="ResourceSpecification"/>.</param>
		/// <returns></returns>
		public Resource GetBy(ResourceSpecification specification)
		{
			return this.ResourceRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the resource by the specified id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public Resource GetById(int id)
		{
			return this.ResourceRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the resources.
		/// </summary>
		/// <returns></returns>
		public IList<Resource> GetAll()
		{
			return this.ResourceRepository.GetAll();
		}

		/// <summary>
		/// Gets all the resources by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="ResourceSpecification"/>.</param>
		/// <returns></returns>
		public IPagedList<Resource, ResourceSpecification> GetPaged(ResourceSpecification specification)
		{
			return this.ResourceRepository.GetPaged(specification);
		}

		/// <summary>
		/// Creates the resource.
		/// </summary>
		/// <returns></returns>
		public Resource Create()
		{
			return new Resource();
		}

		/// <summary>
		/// Initializes this instance.
		/// </summary>
		public void Init()
		{
			var values = this.GetAll();
			var resources = new Resources();

			resources.Init(values);
		}

		/// <summary>
		/// Rebuilds this instance.
		/// </summary>
		public void Rebuild()
		{
			var values = this.GetAll();
			var resources = new Resources();

			resources.Rebuild(values);
		}

		/// <summary>
		/// Inserts the specified resource.
		/// </summary>
		/// <param name="resource">The <see cref="Resource"/>.</param>
		public void Insert(Resource resource)
		{
			this.ResourceRepository.Insert(resource);
		}

		/// <summary>
		/// Inserts the specified resource.
		/// </summary>
		/// <param name="resource">The resource.</param>
		/// <param name="value">The value.</param>
		public void Insert(Resource resource, ResourceCreate value)
		{
			value.Validate();

			resource.LanguageId = value.LanguageId;
			resource.Key = value.ItemKey;
			resource.Value = value.ItemValue;

			this.Insert(resource);
		}

		/// <summary>
		/// Updates the specified resource.
		/// </summary>
		/// <param name="resource">The <see cref="Resource"/>.</param>
		public void Update(Resource resource)
		{
			this.ResourceRepository.Update(resource);
		}

		/// <summary>
		/// Updates the specified resource.
		/// </summary>
		/// <param name="resource">The resource.</param>
		/// <param name="value">The value.</param>
		public void Update(Resource resource, ResourceUpdate value)
		{
			value.Validate();

			resource.LanguageId = value.LanguageId;
			resource.Key = value.ItemKey;
			resource.Value = value.ItemValue;

			this.Update(resource);
		}

		/// <summary>
		/// Deletes the specified resource.
		/// </summary>
		/// <param name="resource">The <see cref="Resource"/>.</param>
		public void Delete(Resource resource)
		{
			this.ResourceRepository.Delete(resource);
		}
	}
}